﻿@using NewLife;
@using NewLife.Web;
@using NewLife.Cube;
@using XCode;
@using XCode.Code
@using XCode.DataAccessLayer
@{
    var fact = ViewBag.Factory as IEntityFactory;
    var page = ViewBag.Page as Pager;
    var fi = fact.MasterTime;
    var fields = ViewBag.SearchFields as FieldCollection ?? new(ViewKinds.Search);

    var dic = new Dictionary<Int32, String>();
    dic[1] = "是";
    dic[0] = "否";

    var atts = new Dictionary<String, Object>();
    if (!atts.ContainsKey("class")) atts.Add("class", "form-control");

}
@foreach (SearchField field in fields)
{
    var name = field.Name.ToLower();
    if (!field.View.IsNullOrEmpty())
    {
        <div class="form-group">
            @if (!field.DisplayName.IsNullOrEmpty())
            {
                <label for="@name" class="control-label">@field.DisplayName：</label>
            }
            @await Html.PartialAsync(field.View, field)
        </div>
    }
    else if (field.Name.EqualIgnoreCase("Enable") && field.Type == typeof(Boolean))
    {
        @await Html.PartialAsync("_Enable")
    }
    else if (field.Type == typeof(Boolean))
    {
        <div class="form-group">
            <label for="@name" class="control-label">@field.DisplayName：</label>
            @Html.ForDropDownList(name, dic, page[name], "全部", true)
        </div>
    }
    else if (field.ItemType.EqualIgnoreCase("area4"))
    {
        @await Html.PartialAsync("_Area4", "")
    }
    else if (field.Name.EqualIgnoreCase("DistrictId") || field.ItemType.EqualIgnoreCase("area3", "area"))
    {
        @await Html.PartialAsync("_Area3", "")
    }
    else if (field.Name.EqualIgnoreCase("CityId") || field.ItemType.EqualIgnoreCase("area2"))
    {
        @await Html.PartialAsync("_Area2", "")
    }
    else if (field.Name.EqualIgnoreCase("ProvinceId") || field.ItemType.EqualIgnoreCase("area1"))
    {
        @await Html.PartialAsync("_Area1", "")
    }
    else if (field.Type.IsInt())
    {
        // 枚举字段
        if (field.Type.IsEnum)
        {
            if (field.Multiple)
            {
                <label for="@name" class="control-label">@field.DisplayName：</label>
                @await Html.PartialAsync("_Form_ListBox", new ListBoxModel(name, EnumHelper.GetDescriptions(field.Type), page[name]))
            }
            else
            {
                <div class="form-group">
                    <label for="@name" class="control-label">@field.DisplayName：</label>
                    @Html.ForDropDownList(name, EnumHelper.GetDescriptions(field.Type), page[name], "全部", true)
                </div>
            }
        }
    }
    // else if (field.Type == typeof(String))
    // {
    //     <div class="form-group">
    //         <label for="@name" class="control-label">@field.DisplayName：</label>
    //         @Html.TextBox(name, page[name], null, atts)
    //     </div>
    // }
}

@if (fi != null)
{
    @await Html.PartialAsync("_DateRange")
}
